package util;


import constant.Constant;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;
import java.util.Set;

/**
 * Created by chenkuo on 2017/9/19.
 */
public class ConfigLoader {

    private static Properties properties = new Properties();
    public static void load(String mode) {
        try {
            properties.load(ConfigLoader.class.getClassLoader().getResourceAsStream(mode));
            init();
        } catch (Exception e) {
            System.out.println("hhhhh" + e.getMessage());
            ConfigLoader.class.getClassLoader().getResourceAsStream(mode);
            e.printStackTrace();
        }
    }

    public static void init(){
        Set<String> strings = properties.stringPropertyNames();
        Class<Constant> constantClass = Constant.class;
        Field[] fields = constantClass.getFields();
        for (Field field : fields) {
            String name = field.getName();
            for (String key : strings) {
                if(name.equals(key)){
                    try {
                        field.set(null,properties.get(key));
                        break;
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
